package com.influxdb.demo.controller;

import com.influxdb.demo.dataObject.Customer;
import com.influxdb.demo.utils.InfluxdbUtils;
import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.*;

@RestController
public class DemoController {
    @Autowired
    private InfluxdbUtils influxdbUtils;
//    @Autowired
//    private InfluxDBConnectPool influxDBConnectPool;

    @RequestMapping(value = "/demo",method = RequestMethod.GET)
    public Object demo(){
        Customer customer=new Customer().setLevel("1").setMsg("测试mode")
                .setName("zb");
        Point point=Point.measurementByPOJO(customer.getClass())
                .addFieldsFromPOJO(customer)
                .time(Long.getLong("1597472023623"),TimeUnit.MILLISECONDS)
                .build();
        System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
//        InfluxDB influxDB = influxdbUtils.getInfluxDB();
//        influxDB.write(point);
        return "influxdb插入成功";
    }

    @RequestMapping(value = "/batchInsert",method = RequestMethod.GET)
    public Object batchInsert() throws InterruptedException {
        System.out.println("开始插入"+LocalDateTime.now());

//        Callable<Integer> task=null;
//        List<Callable<Integer>> tasks=new ArrayList<>();
//        List<Integer> success=new ArrayList<>();
//        for (int i=0;i<10;i++){
//            String num=String.valueOf(i);
//            task=new Callable<Integer>() {
//                @Override
//                public Integer call() throws Exception {
////                    InfluxDB influxDB = influxDBConnectPool.getInfluxDB();
//                    InfluxDB influxDB = influxdbUtils.getInfluxDB();
////                    influxDB.enableBatch(20,1000,TimeUnit.MILLISECONDS);
//                    BatchPoints batchPoints=BatchPoints.builder()
//                            .consistency(InfluxDB.ConsistencyLevel.ALL)
//                            .build();
//                    for (int j=0;j<1000;j++){
//                        Point point=Point.measurementByPOJO(Customer.class)
//                                .addFieldsFromPOJO(
//                                        new Customer().setName(UUID.randomUUID().toString())
//                                        .setLevel(num).setMsg(Thread.currentThread().getName())
//                                )
//                                .time(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(),TimeUnit.MILLISECONDS)
//                                .build();
//                        batchPoints.point(point);
//                    }
//                    influxDB.write(batchPoints);
//                    return 1;
//                }
//            };
//            tasks.add(task);
//        }
//        ExecutorService executorService= Executors.newFixedThreadPool(10);
//        executorService.invokeAll(tasks);
//        executorService.shutdown();


//        InfluxDB influxDB = influxDBConnectPool.getInfluxDB();
        InfluxDB influxDB=influxdbUtils.getInfluxDB();
//        influxDB.enableBatch(20, 10000, TimeUnit.MILLISECONDS);
        BatchPoints batchPoints = BatchPoints.builder()
                .consistency(InfluxDB.ConsistencyLevel.ALL)
                .build();
        for (int j = 0; j < 10000; j++) {
            Point point = Point.measurementByPOJO(Customer.class)
                    .addFieldsFromPOJO(
                            new Customer().setName(UUID.randomUUID().toString())
                                    .setLevel(String.valueOf(j)).setMsg(Thread.currentThread().getName())
                    )
                    .time(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(), TimeUnit.MILLISECONDS)
                    .build();
            batchPoints.point(point);
        }
        influxDB.write(batchPoints);
        System.out.println("插入完成"+LocalDateTime.now());
        return "批量插入成功";
    }

    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public Object search(String name){
        String sql="select count(*) from customer";
//        InfluxDB influxDB = influxdbUtils.getInfluxDB();
//        Query query=new Query(sql);
//        QueryResult rs = influxDB.query(query);
//        List<QueryResult.Result> list = rs.getResults();
//        list.forEach(e-> System.out.println(e));
        return null;
    }

}
